package cn.piesat.springsecurity.exception;


import cn.piesat.springsecurity.base.entity.BaseResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

/**
 * @author HuangAnting
 * @Description 全局异常处理器
 * @date 2021/9/18 17:38
 */
@RestControllerAdvice
public class GlobalExceptionHandler {
    private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
    /**
     *
     * @Description 拦截自定义异常
     * @author HuangAnting
     * @date 2021/10/29 15:04
     * @param e
     * @param request
     * @return cn.piesat.commons.Response
    */
    @ExceptionHandler(BaseException.class)
    public Object handleRuntimeException(BaseException e, HttpServletRequest request) {
        String requestURI = request.getRequestURI();
        log.error("请求地址'{}',发生异常.", requestURI, e);
        return  BaseResponse.fail(e.getCode(),e.getMessage());
    }
}
